home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / identity.pro < prev    next >
Text File  |  1997-07-08  |  2KB  |  65 lines

  1. ; $Id: identity.pro,v 1.5 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1996-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       IDENTITY
  8. ;
  9. ; PURPOSE:
  10. ;       This function returns an N by N identity array, an array with
  11. ;       ones along the main diagonal and zeros elsewhere.
  12. ;
  13. ; CATEGORY:
  14. ;       Linear Algebra.
  15. ;
  16. ; CALLING SEQUENCE:
  17. ;       Result = IDENTITY(N)
  18. ;
  19. ; INPUTS:
  20. ;       N:      The desired column and row dimensions.
  21. ;
  22. ; KEYWORD PARAMETERS:
  23. ;       DOUBLE: If set to a non-zero value, a double precision identity array
  24. ;               is returned.
  25. ;
  26. ; EXAMPLE:
  27. ;       Define an array, A.
  28. ;         A = [[ 2.0,  1.0,  1.0, 1.5], $
  29. ;              [ 4.0, -6.0,  0.0, 0.0], $
  30. ;              [-2.0,  7.0,  2.0, 2.5], $
  31. ;              [ 1.0,  0.5,  0.0, 5.0]]
  32. ;       Compute the inverse of A using the INVERT function.
  33. ;         Inverse = INVERT(A)
  34. ;
  35. ;       Verify the accuracy of the computed inverse using the mathematical
  36. ;       identity, A x A^-1 - I(4) = 0; where A^-1 is the inverse of A, I(4)
  37. ;       is the 4 by 4 identity array and 0 is a 4 by 4 array of zeros.
  38. ;         PRINT, A ## Inverse - IDENTITY(4)
  39. ;
  40. ; REFERENCE:
  41. ;       ADVANCED ENGINEERING MATHEMATICS (seventh edition)
  42. ;       Erwin Kreyszig
  43. ;       ISBN 0-471-55380-8
  44. ;
  45. ; MODIFICATION HISTORY:
  46. ;       Written by:  GGS, RSI, January 1996
  47. ;-
  48.  
  49. FUNCTION Identity, N, Double = Double
  50.  
  51.   ON_ERROR, 2
  52.   if N le 0 then MESSAGE, "N parameter must be greater than 0."
  53.  
  54.   if KEYWORD_SET(Double) eq 0 then begin
  55.     Array = FLTARR(N, N) 
  56.     Array[LINDGEN(N) * (N+1)] = 1.0
  57.   endif else begin
  58.     Array = DBLARR(N, N)
  59.     Array[LINDGEN(N) * (N+1)] = 1.0d
  60.   endelse
  61.  
  62.   RETURN, Array
  63. end
  64.